package com.zerdoor.controller;

import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.ArtArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2015/1/31.
 */
@Controller
@RequestMapping("artArticle")
public class ArtArticleController {

    @Autowired
    private ArtArticleService artArticleService;

    /**
     * 跳转到文章管理页面
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(Model model){
        model.addAttribute("types",artArticleService.getArtArticleType());
        return "/artArticle/index";
    }

    /**
     * 获取文章列表
     * @param request
     * @param page 当前页
     * @param rows 页条数
     * @return
     */
    @RequestMapping(value = "/getArtArticle",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getArtArticle(HttpServletRequest request,int page,int rows){

        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        params.put("type",request.getParameter("type"));
        List<Map<String, Object>> list = artArticleService.getArtArticle(params);
        PageInfo pageInfo = new PageInfo(list);
        result.put("rows",list);
        result.put("total",pageInfo.getTotal());
        return result;
    }

    /**
     * 添加文章
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> add(HttpServletRequest request){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("article_id",null);
        params.put("title", request.getParameter("title"));
        params.put("context",request.getParameter("context"));
        params.put("type",Integer.parseInt(request.getParameter("type")));
        params.put("artStatus",Integer.parseInt(request.getParameter("artStatus")));
        try {
            artArticleService.addArtArticle(params);
        } catch (Exception e) {
            result.put("errormsg","添加文章失败");
        }
        return result;
    }


    @RequestMapping(value = "/edit",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> edit(HttpServletRequest request){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("articleId",Integer.parseInt(request.getParameter("id")));
        params.put("title", request.getParameter("title"));
        params.put("context",request.getParameter("context"));
        params.put("type",Integer.parseInt(request.getParameter("type")));
        params.put("artStatus",Integer.parseInt(request.getParameter("artStatus")));
        try {
            artArticleService.updateArtArticle(params);
        } catch (Exception e) {
            result.put("errormsg","添加文章失败");
        }
        return result;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(int id) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            artArticleService.deleteArtArticle(id);
        } catch (Exception e) {
            result.put("errormsg", "删除文章失败");
        }
        return result;
    }

    @RequestMapping(value = "/artArticle",method = RequestMethod.POST)
     @ResponseBody
     public Map<String, Object> artArticle(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("articleId",id);
        try {
            artArticleService.updateActivationArtArticle(result);
        } catch (Exception e) {
            result.put("errormsg","激活失败");
        }
        return result;
    }

    @RequestMapping(value = "/stop",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> stop(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("articleId",id);
        try {
            artArticleService.updateStopArtArticle(result);
        } catch (Exception e) {
            result.put("errormsg","停用失败");
        }
        return result;
    }
}
